package ai.people.netmon.telegram.controller;

//import ai.people.core.security.support.RequestHolderUtil;
import ai.people.netmon.framework.domain.telegram.entity.Channels;
import ai.people.netmon.framework.domain.telegram.entity.Topic;
import ai.people.netmon.framework.domain.telegram.entity.Users;
import ai.people.netmon.framework.domain.telegram.vo.channels.ChatMessage;
import ai.people.netmon.framework.domain.telegram.vo.topic.AnalyzedTopic;
import ai.people.netmon.framework.model.response.ListResponse;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.ResponseResult;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.telegram.domain.request.AnalyzedListRequest;
import ai.people.netmon.telegram.domain.request.topic.RelatedMessageRequest;
import ai.people.netmon.telegram.domain.request.topic.RelatedTopicRequest;
import ai.people.netmon.telegram.service.TopicAnalysisService;
import com.people.core.validator.group.AddGroup;
import com.people.core.validator.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 主题分析控制器
 *
 * @author yuanqinglong
 * @date 2022/1/14 18:18
 */
@Validated
@RestController
@RequestMapping("/topic/analysis")
public class TopicAnalysisController {


    @Autowired
    private TopicAnalysisService topicAnalysisServiceImpl;


    /**
     * 获取主题
     *
     * @param topicId 主题id
     * @return {@link Result}<{@link Topic}>
     */
    @GetMapping("/{topicId}")
    public Result<Topic> getTopic(@PathVariable Long topicId) {
        return ResultUtil.success(topicAnalysisServiceImpl.getTopic(topicId));
    }


    /**
     * 修改主题
     *
     * @param topic 主题
     * @return {@link ResponseResult}
     */
    @PutMapping
    public Result<?> updateTopic(@Validated(value = UpdateGroup.class) @RequestBody Topic topic) {
        topicAnalysisServiceImpl.updateTopic(topic);
        return ResultUtil.success();
    }

    /**
     * 添加主题
     *
     * @param topic 主题
     * @return {@link ResponseResult}
     */
    @PostMapping
    public Result<?> addTopic(@Validated(value = AddGroup.class) @RequestBody Topic topic) {
        topicAnalysisServiceImpl.addTopic(topic);
        return ResultUtil.success();
    }


    /**
     * 相关群组
     *
     * @param relatedTopicRequest 相关群组请求参数
     * @return {@link Result}<{@link PageResponse}<{@link Channels}>>
     */
    @PostMapping("/relatedChannels")
    public Result<PageResponse<Channels>> relatedChannels(@Valid @RequestBody RelatedTopicRequest relatedTopicRequest) {
        return ResultUtil.success(topicAnalysisServiceImpl.relatedChannels(relatedTopicRequest));
    }


    /**
     * 相关账号
     *
     * @param relatedTopicRequest 相关账号请求参数
     * @return {@link Result}<{@link PageResponse}<{@link Users}>>
     */
    @PostMapping("/relatedAccounts")
    public Result<PageResponse<Users>> relatedAccounts(@Valid @RequestBody RelatedTopicRequest relatedTopicRequest) {
        return ResultUtil.success(topicAnalysisServiceImpl.relatedAccount(relatedTopicRequest));
    }


    /**
     * 相关信息
     *
     * @param relatedMessageRequest 相关信息请求参数
     * @return {@link Result}<{@link PageResponse}<{@link ChatMessage}>>
     */
    @PostMapping("/relatedMessage")
    public Result<PageResponse<ChatMessage>> relatedMessage(@Valid @RequestBody RelatedMessageRequest relatedMessageRequest) {
        PageResponse<ChatMessage> chatMessagePageResponse = topicAnalysisServiceImpl.relatedMessage(relatedMessageRequest);
        return ResultUtil.success(chatMessagePageResponse);
    }


    /**
     * 删除主题
     *
     * @param topicId 主题id
     * @return {@link ResponseResult}
     */
    @DeleteMapping("/{topicId}")
    public Result<?> deleteTopic(@PathVariable Long topicId) {
        topicAnalysisServiceImpl.deleteTopic(topicId);
        return ResultUtil.success();
    }


    /**
     * 已添加分析主题
     *
     * @param analyzedListRequest 分析列表请求
     * @return {@link Result}<{@link ListResponse}<{@link AnalyzedTopic}>>
     */
    @PostMapping("/analyzedTopics")
    public Result<ListResponse<AnalyzedTopic>> analyzedTopics(@Valid @RequestBody AnalyzedListRequest analyzedListRequest) {
        //analyzedListRequest.setUserId(RequestHolderUtil.getUserId());
        return ResultUtil.success(ListResponse.builder(topicAnalysisServiceImpl.analyzedTopics(analyzedListRequest)));
    }


}
